package net.betou.trade.entity.main;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import net.betou.trade.entity.main.base.BaseAgentAndAdmins;

import org.apache.commons.lang.StringUtils;

/**
 * 居间商信息表
 * 
 * @author 叶装装
 * 
 */
public class AgentAndAdmins extends BaseAgentAndAdmins{

	public AgentAndAdmins( ) {
		super( );
	}
	
	/**
	 * 获取短信数量
	 * @return
	 */
	public Integer getSmsNum( ){
		Funds funds = getFundsAccount( );
		if( funds == null ){
			return 0;
		}
		
		return funds.getSmsNum( );
	}
	
	public Funds getFundsAccount( ){
		Set< Funds > set = getFunds( );
		if( set == null || set.isEmpty( ) ){
			return null;
		}
		
		return set.iterator( ).next( );
	}
	
	/**
	 * @deprecated
	 * @return
	 */
	public Set< AgentAndAdmins > getProxyList( ){
		Set< AgentAndAdmins > childs = this.getChilds( );
		
		if( childs == null || childs.size( ) == 0 ){
			return null;
		}
	
		Set< AgentAndAdmins > proxyList = new HashSet< AgentAndAdmins >( );
		
		for( Iterator< AgentAndAdmins > it = childs.iterator( ); it.hasNext( ); ){
			AgentAndAdmins admin = it.next( );
			
			if( !admin.getProxy( ) ){
				continue;
			}
			
			proxyList.add( admin );
		}
		
		return proxyList;
		
	}
	
	// 判断是否是机构
	public boolean getAgency( ){
		return this.getAgencyOrProxy( ) && !this.getJustAdmin( ) && !this.getSystem( );
	}
	
	// 判断是否是代理商
	public boolean getProxy( ){
		return !this.getAgencyOrProxy( ) && !this.getJustAdmin( ) && !this.getSystem( );
	}
	
	// 获取机构列表
	public int getAgencyCount( ){
		
		Set< AgentAndAdmins > childs = getChilds( );
		
		if( childs == null || childs.isEmpty( ) ){
			return 0;
		}
		
		int agencySize = 0;
		
		Iterator< AgentAndAdmins > it = childs.iterator( );
		
		while( it.hasNext( ) ){
			AgentAndAdmins admin = it.next( );
			
			if( admin.getSystem( ) ){
				continue;
			}
			
			if(! admin.getJustAdmin( ) && admin.getAgencyOrProxy( ) ){
				agencySize ++;
			}
		}
		
		return agencySize;
	}
	
	public int getAccountCount( ){
		
		Set< AgentAndAdmins > childs = getChilds( );
		
		if( childs == null || childs.isEmpty( ) ){
			return 0;
		}
		
		int accountSize = 0;
		
		Iterator< AgentAndAdmins > it = childs.iterator( );
		
		while( it.hasNext( ) ){
			AgentAndAdmins admin = it.next( );
			
			if( admin.getSystem( ) ){
				continue;
			}
			accountSize ++;
		}
		
		return accountSize;
	}
	
	public int getChildCount( ){
		Set< AgentAndAdmins > childs = getChilds( );
		
		if( childs == null || childs.isEmpty( ) ){
			return 0;
		}
		
		int notAdminSize = 0;
		
		Iterator< AgentAndAdmins > it = childs.iterator( );
		
		while( it.hasNext( ) ){
			AgentAndAdmins admin = it.next( );
			
			if(! admin.getJustAdmin( ) && !admin.getAgencyOrProxy( ) ){
				notAdminSize ++;
			}
		}
		
		return notAdminSize;
	}
	
	public int getUserCount( ){
		
		Set< User > users = getUsers( );
		
		if( users == null ){
			return 0;
		}
		
		return users.size( );
	}
	
	/**
	 * 获取系统查询设置的角色，如果有多个，将会只获取等级较小的角色信息
	 * @return
	 */
	public Role getSearchSettingRole( ){
		Set< Role > set = getRoles( );
		
		if( set == null || set.isEmpty( ) ){
			return null;
		}
		
		// 如果只有一个角色，直接返回
		if( set.size( ) == 1 ){
			return set.iterator( ).next( );
		}
		
		// 如果有多个进行筛选处理获取等级较低的那个

		int temp = 0;
		int i = 0;
		
		// 寻找最小level
		for( Role r : set ){
			Integer level = r.getLevel( );
			if( i == 0 ){
				temp = level;
			}else{
				temp = temp < level ? temp : level ;
			}
			i++;
		}
		// 根据最小level 得到
		for( Role r : set ){
			Integer level = r.getLevel( );
			if( level == temp ){
				return r;
			}
		}
		
		return null;
	}
	
	public void addToPicture( Picture p ){
		if( p == null ){
			return;
		}
		
		Set< Picture > pics = getPics( );
		
		if( pics == null ){
			pics = new HashSet< Picture >( );
			setPics( pics );
		}
		
		pics.add( p );
	}
	
	public void addToRoles( Role role ) {
		if( role == null ){
			return;
		}

		Set< Role > set = getRoles( );

		if( set == null ){
			set = new HashSet< Role >( );
			setRoles( set );
		}
		set.add( role );

	}

	/**
	 * 获取权限
	 * @return
	 */
	public Set< String > getPerms( ) {
		Set< Role > roles = getRoles( );
		if( roles == null ){
			return null;
		}
		Set< String > allPerms = new HashSet< String >( );
		for ( Role role : getRoles( ) ){
			allPerms.addAll( role.getPerms( ) );
		}
		return allPerms;
	}
	
	public void saveToAdmin( AgentAndAdmins currentAdmin ){
		
		this.setCreateDate( new Date( ) );
		this.setLoginDate( null );
		this.setJustAdmin( true ); 
		this.setLoginCount( 0 );
		if( getDisabled( ) == null ){
			this.setDisabled( true ); // 表示不启用, 已禁用
		}

		if( StringUtils.isBlank( getEmail( ) ) ){
			this.setEmail( null );
		}

		if( StringUtils.isBlank( getMobile( ) ) ){
			setMobile( null );
		}
		
		this.setSystem( false );
		
		Integer deep = currentAdmin.getDeep( );
		
		this.setDeep( deep + 1 ); // 深度自加
		
		String topIntermedia = currentAdmin.getTopIntermedia( );
		
		if( "0".equals( topIntermedia )){
			topIntermedia = String.valueOf( currentAdmin.getId( ) ); // 如果是0，表示最顶级， 这个top, 应该为当前的ID
		}else{ // 否则的话为，当前的top + 当前添加的ID
			topIntermedia = currentAdmin.getTopIntermedia( ) + "," + currentAdmin.getId( ); 
		}
		
		this.setTopIntermedia( topIntermedia );
		
		this.setParent( currentAdmin );
		
		
	}
	
	public void saveToSystem( ){
		init( );
		this.setJustAdmin( true );
		this.setSystem( true );
		this.setAgencyOrProxy( false );
	}
	
	/**
	 * 添加系统管理员
	 * @deprecated
	 */
	public void initToSaveAdmin( ) {
		init( );
		this.setJustAdmin( true ); // justAdmin 属性对系统管理员不起作用
		this.setSystem( true );
	}
	
	/**
	 * 系统管理员添加居间
	 */
	public  void initToSaveAgent( ){
		init( );
		this.setJustAdmin( false ); // justAdmin 属性对系统管理员不起作用
		this.setSystem( false );
	}
	
	public void saveToProxy( AgentAndAdmins currentAdmin ){
		this.setAgencyOrProxy( false );
		init( currentAdmin );
	}
	
	/**
	 * @param currentAdmin
	 */
	public void saveToAgency( AgentAndAdmins currentAdmin ){
		
		this.setAgencyOrProxy( true );
		
		// 如果是系统管理员,就表示添加的是最最顶级的
		if( currentAdmin.getSystem( ) ){
			this.setJustAdmin( false );
			this.setSystem( false );
			init( );
		}else{
			init( currentAdmin );
		}
		
	}
	
	private void init( AgentAndAdmins currentAdmin ){
		this.setJustAdmin( false );
		this.setSystem( false );
		this.setCreateDate( new Date( ) );
		this.setLoginDate( null );
		this.setLoginCount( 0 );
		if( getDisabled( ) == null ){
			this.setDisabled( true ); // 表示不启用, 已禁用
		}

		if( StringUtils.isBlank( getEmail( ) ) ){
			this.setEmail( null );
		}

		if( StringUtils.isBlank( getMobile( ) ) ){
			setMobile( null );
		}
		
		Integer deep = currentAdmin.getDeep( );
		
		this.setDeep( deep + 1 ); // 深度自加
		
		String topIntermedia = currentAdmin.getTopIntermedia( );
		
		if( "0".equals( topIntermedia )){
			topIntermedia = String.valueOf( currentAdmin.getId( ) ); // 如果是0，表示最顶级， 这个top, 应该为当前的ID
		}else{ // 否则的话为，当前的top + 当前添加的ID
			topIntermedia = currentAdmin.getTopIntermedia( ) + "," + currentAdmin.getId( ); 
		}
		
		this.setTopIntermedia( topIntermedia );
		
		this.setParent( currentAdmin );
		
	}
	
	/**
	 * 居间商添加居间商时候初始化
	 * @deprecated
	 */
	public void initToSaveAgent(  AgentAndAdmins currentAdmin ){
		this.setCreateDate( new Date( ) );
		this.setLoginDate( null );
		this.setJustAdmin( false ); 
		this.setLoginCount( 0 );
		if( getDisabled( ) == null ){
			this.setDisabled( true ); // 表示不启用, 已禁用
		}

		if( StringUtils.isBlank( getEmail( ) ) ){
			this.setEmail( null );
		}

		if( StringUtils.isBlank( getMobile( ) ) ){
			setMobile( null );
		}
		
		this.setSystem( false );
		
		Integer deep = currentAdmin.getDeep( );
		
		this.setDeep( deep + 1 ); // 深度自加
		
		String topIntermedia = currentAdmin.getTopIntermedia( );
		
		if( "0".equals( topIntermedia )){
			topIntermedia = String.valueOf( currentAdmin.getId( ) ); // 如果是0，表示最顶级， 这个top, 应该为当前的ID
		}else{ // 否则的话为，当前的top + 当前添加的ID
			topIntermedia = currentAdmin.getTopIntermedia( ) + "," + currentAdmin.getId( ); 
		}
		
		this.setTopIntermedia( topIntermedia );
		
		this.setParent( currentAdmin );
		
	}
	
	private void init( ){
		this.setCreateDate( new Date( ) );
		this.setDeep( 0 );
		this.setLoginDate( null );
		this.setTopIntermedia( "0" );
		this.setLoginCount( 0 );
		if( getDisabled( ) == null ){
			this.setDisabled( true ); // 表示不启用, 已禁用
		}

		if( StringUtils.isBlank( getEmail( ) ) ){
			this.setEmail( null );
		}

		if( StringUtils.isBlank( getMobile( ) ) ){
			setMobile( null );
		}
	}

	public AgentAndAdminsExt getExt( ) {
		Set< AgentAndAdminsExt > set = this.getAdminExt( );
		if( set != null && set.size( ) > 0 ){
			return set.iterator( ).next( );
		}else{
			return null;
		}
	}
	
	public Config getConfig( ){
		Set< Config > configs = this.getConfigs( );
		if( configs != null && configs.size( ) > 0 ){
			return configs.iterator( ).next( );
		}
		
		return null;
		
	}

	private static final long serialVersionUID = 8904750980284610242L;
}
